# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2023/4/25 14:50
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : window_algo.py
# @Version: OpenPlanner2.0
import time

from OpenPlanner.core.api.solver_interface import PlanningStrategy
from OpenPlanner.examples.window_demo.window_algo_utils import construct_constraints, add_and_solve_constraints, \
    write_declare_set_to_txt


class GCLAlgo(PlanningStrategy):
    def do_planning(self):
        link_obj_set = self.link_array
        stream_obj_set = self.stream_array
        stream_instance_obj_set = self.instance_array

        sync_precision = self.kwargs['sync_precision']
        timeout = self.kwargs['timeout']

        constraint_set = construct_constraints(
            link_obj_set,
            stream_obj_set,
            stream_instance_obj_set,
            sync_precision
        )
        result_set = \
            add_and_solve_constraints(
                constraint_set,
                timeout=timeout)

        # cur_time = \
        #     time.strftime('%Y%m%d_%H%M%S',
        #                   time.localtime(time.time()))
        cur_time = ''
        dir = self.solution_txt.split('/')[:-1]
        fn = self.solution_txt.split('/')[-1]
        fn = cur_time + fn

        solution_txt = ''
        for item in dir:
            item = item + '/'
            solution_txt += item
        solution_txt += fn

        write_declare_set_to_txt(result_set, link_obj_set, solution_txt)
